<template>
  <div class="login_body">
    <!-- <div class="bg" /> -->
    <div class="box">
      <div class="title">成绩管理系统-登录</div>
      <el-form ref="form" :model="loginForm" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username" clearable />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" show-password clearable />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="handleLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import {loginAPI} from '@/api/user'
import { FORM_KEY,USER_NAME } from "@/constants/KEY";
import { loginAPI } from "@/api/user";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rememberMe: true,
      rules: {
        username: [
          { required: true, message: "用户名不可为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不可为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    const loginData = localStorage.getItem(FORM_KEY);
    //先判断是否为空
    if (loginData) {
      const { username, password } = JSON.parse(loginData);
      this.loginForm.username = username;
      this.loginForm.password = password;
    }
  },
  methods: {
    handleExportExcel() {
      exportExcelAPI(this.params)
        .then(response => {
          const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

          const link = document.createElement('a');
          link.href = window.URL.createObjectURL(blob);
          link.download = 'scores.xlsx';
          link.click();

          window.URL.revokeObjectURL(link.href);
        })
        .catch(error => {
          console.error('Failed to export Excel:', error);
        });
    },
    handleLogin() {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return;
        //提交接口 不规范
        const res = await loginAPI(this.loginForm);
        if (res.code === 1) {
          //判断用户是否点击记住我
          if (this.rememberMe) {
            localStorage.setItem(FORM_KEY, JSON.stringify(this.loginForm));
          } else {
            localStorage.removeItem(FORM_KEY);
          }
          //添加token信息
          await this.$store.commit("user/setToken", res.data);
          localStorage.setItem(USER_NAME,this.loginForm.username)
          this.$message.success("登录成功");
          this.$router.push('/')
          // // 定义并调用等待1秒的函数
          // await new Promise((resolve) => setTimeout(resolve, 1000));
          // location.reload();
        }
        // this.$store.commit('user/setToken',res.data.token)
        // await this.$store.dispatch('user/loginAction',this.loginForm)
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative; /* 添加相对定位 */
}

.bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom right, #4b79a1, #283e51); /* 添加渐变背景色 */
  opacity: 0.8; /* 设置透明度 */
}

.box {
  width: 400px;
  padding: 20px;
  border-radius: 8px;
  background-color: #f0f2f5;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 20px;
}

.login_btn {
  width: 100%;
  border-radius: 4px;
}
</style>
